package com.example.sharingjdbc4digiwin.config;

import com.google.common.collect.Range;
import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.RangeShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import io.shardingsphere.api.algorithm.sharding.standard.RangeShardingAlgorithm;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @author： ygl
 * @date： 2019/06/23
 * @Description：
 */
@Slf4j
public class DataSourceAlg implements PreciseShardingAlgorithm<String>, RangeShardingAlgorithm<String> {

    /**
     * @param names  匹配的库
     * @param shardingValue  对应的分片
     * @return
     */
    @Override
    public String doSharding(Collection<String> names, PreciseShardingValue<String> shardingValue) {
        log.info("分库算法参数 {},{}",names,shardingValue);
        int datasourceIndex = 1;
        String dataSource = "ds_" + datasourceIndex;
        log.info("对应的分库：{}",dataSource);
        return dataSource;
    }

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<String> shardingValue) {
        log.info("分库范围分片算法参数 {},{}", availableTargetNames, shardingValue);
        Collection<String> shardingList = new ArrayList<>();
        String dataSource1 = "ds_1";
        shardingList.add(dataSource1);
        log.info("分库范围分片算法对应的分库：{}",shardingList);
        return shardingList;
    }

}
